package javaNIO1.Selectors;


import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.LineNumberReader;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created with Intel IDEA.
 * 面向对象的文件Grep
 *
 * @Auther: gjt
 * @Date: 2024/8/3 12:58
 */
public class Grep {
    // 用于该实例的模式
    private Pattern pattern;

    public Grep(Pattern pattern) {
        this.pattern = pattern;
    }

    public Grep(String regex, boolean ignoreCase) {
        this.pattern = Pattern.compile(regex, ignoreCase ? Pattern.CASE_INSENSITIVE : 0);
    }

    public MatchedLine[] grep(File file) throws IOException {
        List list = grepList(file);
        MatchedLine[] matchedLines = new MatchedLine[list.size()];
        list.toArray(matchedLines);
        return matchedLines;
    }


    public MatchedLine[] grep(String fileName) throws IOException {
        return grep(new File(fileName));
    }

    public MatchedLine[] grep(File[] files) {
        LinkedList linkedList = new LinkedList();
        try {
            for (int i = 0; i < files.length; i++) {
                List list = grepList(files[i]);

                linkedList.add(list);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        MatchedLine[] matchedLines = new MatchedLine[linkedList.size()];

        linkedList.toArray(matchedLines);

        return matchedLines;
    }

    public static class MatchedLine {
        private File file;
        private int lineNumber;
        private String lineText;
        private int start;
        private int end;

        MatchedLine(File file, int lineNumber, String lineText, int start, int end) {
            this.file = file;
            this.lineNumber = lineNumber;
            this.lineText = lineText;
            this.start = start;
            this.end = end;
        }

        public File getFile() {
            return this.file;
        }

        public int getLineNumber() {
            return this.lineNumber;
        }

        public String getLineText() {
            return this.lineText;
        }

        public int getStart() {
            return this.start;
        }

        public int getEnd() {
            return this.end;
        }
    }

    private List grepList(File file) throws IOException {
        if (!file.exists()) {
            throw new IOException("不存在；" + file);
        }
        if (!file.isFile()) {
            throw new IOException("不是文件；" + file);
        }
        if (!file.canRead()) {
            throw new IOException("不能读取；" + file);
        }
        LinkedList linkedList = new LinkedList();

        FileReader fileReader = new FileReader(file);
        LineNumberReader lineNumberReader = new LineNumberReader(fileReader);
        Matcher matcher = this.pattern.matcher("");

        String line;
        while ((line = lineNumberReader.readLine()) != null) {
            matcher.reset(line);
            if (matcher.find()) {
                linkedList.add(new MatchedLine(file, lineNumberReader.getLineNumber(), line, matcher.start(), matcher.end()));
            }
            lineNumberReader.close();

        }
        return linkedList;
    }


    public static void main(String[] args) {
        // 设置默认值
        boolean ignoreCase = false;
        boolean onebyone = false;

        LinkedList linkedList = new LinkedList();// 采集变量
        for (int i = 0; i < args.length; i++) {
            if (args[i].startsWith("-")) {
                if (args[i].equals("-i") || args[i].equals("--ignore-case")) {
                    ignoreCase = true;
                }
                if (args[i].equals("-1")) {
                    onebyone = true;
                }
                continue;
            }
            linkedList.add(args[i]);
        }
        // 是否有足够的变量可以运行
        if (linkedList.size() < 2) {
            System.out.println("变量不够");
            return;
        }

        // 列表中的第一个变量作为正则模式；将模式以及忽略大小标志的当前值传递给新的Grep 对象
        Grep grep = new Grep((String) linkedList.remove(0), ignoreCase);

        if (onebyone) {
            Iterator iterator = linkedList.iterator();
             // 这都是文件了
            while (iterator.hasNext()) {
                String fileName = (String) iterator.next();
                System.out.println(fileName + ":");
                MatchedLine[] matchedLines = null;
                try {
                    matchedLines = grep.grep(fileName);
                } catch (IOException e) {
                    e.printStackTrace();
                    continue;
                }

                for (int i = 0; i < matchedLines.length; i++) {
                    MatchedLine matchedLine = matchedLines[i];

                    System.out.println(
                            matchedLine.getLineNumber() + ":" +
                                    matchedLine.getStart() + "-" +
                                    (matchedLine.getEnd() - 1) + ":" +
                                    matchedLine.getLineText()
                    );
                }
            }
        } else {
            File[] files = new File[linkedList.size()];
            for (int i = 0; i < files.length; i++) {
                files[i] = new File((String) linkedList.get(i));
            }

            MatchedLine[] grep1 = grep.grep(files);
            for (int i = 0; i < grep1.length; i++) {
                MatchedLine matchedLine = grep1[i];

                System.out.println(
                        matchedLine.getFile().getName() + ":" +
                                matchedLine.getLineNumber() + ":" +
                                matchedLine.getLineText()
                );
            }
        }


    }


}
